<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
    <el-form label-position="right" :model="repairRequestForm" :label-width="formLabelWidth" ref="refRepairRequestForm" :rules="rules">
      <el-row>
        <el-col :span="11">
          <el-form-item label="楼栋名称" prop="buildingName">
            <el-input v-model="repairRequestForm.buildingName" placeholder="请选择楼栋名称" readonly="readonly">
              <i slot="suffix" class="el-input__icon el-icon-search" @click="findBuilding"></i>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="单元房号" prop="roomNumber">
            <el-input v-model="repairRequestForm.roomNumber" placeholder="请选择单元房号" readonly="readonly">
              <i slot="suffix" class="el-input__icon el-icon-search" @click="findRoom"></i>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="问题描述" prop="description">
            <el-input v-model="repairRequestForm.description" placeholder="请输入问题描述" maxlength="65535"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="报修时间" prop="requestTime">
            <el-date-picker
                v-model="repairRequestForm.requestTime" type="datetime" placeholder="报修时间" value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="状态" prop="status">
            <el-select v-model="repairRequestForm.status" placeholder="请选择状态">
              <el-option label="待处理" :value="10"></el-option>
              <el-option label="处理中" :value="20"></el-option>
              <el-option label="已完成" :value="30"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="维修保养人员" prop="staffName">
            <el-input v-model="repairRequestForm.staffName" placeholder="请选择维修保养人员" readonly="readonly">
              <i slot="suffix" class="el-input__icon el-icon-search" @click="findStaff"></i>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="repairRequestForm.remark" placeholder="请输入备注" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="完成时间" prop="finishTime">
            <el-date-picker
                v-model="repairRequestForm.finishTime" type="datetime" placeholder="完成时间" value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="buildingSelectorVisible" v-if="buildingSelectorVisible" title="楼栋信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <building-selector @building-selected-event="handleBuildingSelectedEvent">
      </building-selector>
    </el-dialog>
    <el-dialog :visible.sync="roomSelectorVisible" v-if="roomSelectorVisible" title="单元信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <room-selector @room-selected-event="handleRoomSelectedEvent">
      </room-selector>
    </el-dialog>
    <el-dialog :visible.sync="staffSelectorVisible" v-if="staffSelectorVisible" title="维修保养人员选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <staff-selector @staff-selected-event="handleStaffSelectedEvent">
      </staff-selector>
    </el-dialog>
  </div>
</template>

<script>
import repairRequestApi from '@/api/repairRequestApi'
import BuildingSelector from "@/views/building/BuildingSelector.vue";
import RoomSelector from "@/views/room/RoomSelector.vue";
import StaffSelector from "@/views/staff/StaffSelector.vue";
export default {
  name: 'RepairRequestAdd',
  components: {BuildingSelector,RoomSelector,StaffSelector,},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      repairRequestForm: {
        buildingId: '',
        buildingName: '',
        roomId: '',
        roomNumber: '',
        description: '',
        requestTime: '',
        status: '',
        staffId: '',
        staffName: '',
        remark: '',
        finishTime: ''
      },
      rules: {
        buildingName: [
          { required: true, message: '请输入楼栋名称', trigger: 'blur' }
        ],
        roomNumber: [
          { required: true, message: '请输入单元房号', trigger: 'blur' }
        ],
      },
      buildingSelectorVisible: false,
      roomSelectorVisible: false,
      staffSelectorVisible: false,
    }
  },
  created () {
    if (this.id) {
      repairRequestApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.repairRequestForm = resp.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.repairRequestForm = {
        buildingId: '',
        buildingName: '',
        roomId: '',
        roomNumber: '',
        description: '',
        requestTime: '',
        status: '',
        staffId: '',
        staffName: '',
        remark: '',
        finishTime: ''
      }
      this.$emit('reset-repair-request-add-event')
    },
    onSave () {
      this.$refs.refRepairRequestForm.validate((valid) => {
        if (valid) {
          repairRequestApi.save(this.repairRequestForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-repair-request-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    findBuilding () {
      this.buildingSelectorVisible = true
    },
    handleBuildingSelectedEvent (selectedBuilding) {
      this.buildingSelectorVisible = false
      if (selectedBuilding && 'buildingId' in selectedBuilding && this.repairRequestForm.buildingId !== selectedBuilding['buildingId']) {
        this.repairRequestForm.buildingId = selectedBuilding['buildingId']
        this.repairRequestForm.buildingName = selectedBuilding['buildingName']
      }
    },
    findRoom () {
      this.roomSelectorVisible = true
    },
    handleRoomSelectedEvent (selectedRoom) {
      this.roomSelectorVisible = false
      if (selectedRoom && 'roomId' in selectedRoom && this.repairRequestForm.roomId !== selectedRoom['roomId']) {
        this.repairRequestForm.roomId = selectedRoom['roomId']
        this.repairRequestForm.roomNumber = selectedRoom['roomNumber']
      }
    },
    findStaff () {
      this.staffSelectorVisible = true
    },
    handleStaffSelectedEvent (selectedStaff) {
      this.staffSelectorVisible = false
      if (selectedStaff && 'staffId' in selectedStaff && this.repairRequestForm.staffId !== selectedStaff['staffId']) {
        this.repairRequestForm.staffId = selectedStaff['staffId']
        this.repairRequestForm.staffName = selectedStaff['staffName']
      }
    },
    onBack () {
      this.$emit('close-repair-request-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>